//
//  STWXPublishViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/25.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STWXPublishViewController.h"

#import "AddrChooserViewController.h"

@interface STWXPublishViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) STWXPublishViewModel* viewModel;

@property (nonatomic, weak) IBOutlet UITextField* titleTF;
@property (nonatomic, weak) IBOutlet UITextField* introTF;
@property (nonatomic, weak) IBOutlet UIView* contentTextView;
@property (weak, nonatomic) IBOutlet UIButton* areaBtn;
@property (weak, nonatomic) IBOutlet UILabel* areaLabel;
@property (nonatomic, weak) IBOutlet UIButton* submitBtn;

@end

@implementation STWXPublishViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
}

- (void)initUI
{
    @weakify(self);
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        // ScrollView 内容 未能在此时才约束
        
        //Request
        if (self.viewModel.articleID && [self.viewModel.articleID isNotBlank])
        {
            [self.viewModel.detailCmd execute:nil];
        }
    }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
    
    [[[viewDidLayoutSVSignal take:1] deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self)
         
         //重置 更新 frame
         [self _setupAllView];
     }];
    
#pragma mark -- bindViewModel
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    RACChannelTo(self.viewModel, ptitle) = RACChannelTo(self.titleTF, text);
    RACChannelTo(self.viewModel, pintro) = RACChannelTo(self.introTF, text);
    
    // 地区选择
    [[self.areaBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
     {
         @strongify(self);
         
         [self _push2AddrPop];
     }];
    
    RACSignal* addressChangeSignal = [RACObserve(self.viewModel, area) distinctUntilChanged];
    RAC(self.areaLabel, text) = [[addressChangeSignal replay] map:^id(id value)
                                 {
                                     NSString* strResult = value;
                                     
                                     strResult = HCStrFormat(@"%@ >", strResult);
                                     
                                     return strResult;
                                 }];
    
    self.submitBtn.rac_command = self.viewModel.submitCmd;
    
    [[self.viewModel.submitCmd.executionSignals.switchToLatest deliverOnMainThread] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self.navigationController popViewControllerAnimated:YES];
        
        [MBProgressHUD mh_showTips:x];
    }];
    [[self.viewModel.submitCmd.errors deliverOnMainThread] subscribeNext:^(NSError* error)
     {
         [MBProgressHUD mh_hideHUD];
         
         /// 处理验证错误的error
         if ([error.domain isEqualToString:SUCommandErrorDomain]) {
             [MBProgressHUD mh_showTips:error.userInfo[SUCommandErrorUserInfoKey]];
             return ;
         }
         [MBProgressHUD mh_showErrorTips:error];
     }];
}

#pragma mark - 更新初始化 操作
- (void)_setupAllView
{
    @weakify(self);
    
    YYTextView* tView = [[YYTextView alloc] initWithFrame:(CGRect) {0, 0, self.contentTextView.frame.size}];
    tView.font = [UIFont systemFontOfSize:16.0f];
    tView.placeholderFont = [UIFont systemFontOfSize:16.0f];
    tView.placeholderText = @"关于环保主题的文章、观点或具有参考价值的环保类文献等";
    tView.placeholderTextColor = UIColorHex(#e0e0e0);
    tView.tag = 100;
    [self.contentTextView addSubview:tView];
//    RAC(self.viewModel, content) = RACObserve(tView, text);
    RACChannelTo(self.viewModel, content) = RACChannelTo(tView, text);
}

#pragma mark - Delegate 一些无法用RAC实现
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}

#pragma mark-代理方法
// 设立实现代理，注意要返回UIModalPresentationNone
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

#pragma mark - 功能

#pragma mark - 跳转
- (void)_push2AddrPop
{
    DataManager* dm = [DataManager sharedDataManager];
    
    @weakify(self)
    AddrChooserViewModel* vm = [[AddrChooserViewModel alloc] initWithParams:@{SUViewModelTitleKey:@"选择地区"}];
    vm.chooseBlock = ^(ProvinceListData* choosePData)
    {
        @strongify(self);
        
        [self.viewModel resetZoneData:choosePData];
    };
    AddrChooserViewController* vc = [[AddrChooserViewController alloc] initWithViewModel:vm];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
